//
//  SelectConcludeCell.m
//  Estate
//
//  Created by wangdongdong on 16/9/10.
//  Copyright © 2016年 ttsx. All rights reserved.
//

#define BTN_WIDTH 70
#define SPACING 30
#import "SelectConcludeCell.h"

@implementation SelectConcludeCell

/**位置标签*/
- (UILabel *)title {
    if(_title==nil){
        _title=[[UILabel alloc]init];
        _title.font=[UIFont systemFontOfSize:17.*BIZ];
        _title.textColor=[UIColor colorWithRed:0.54 green:0.54 blue:0.54 alpha:1.00];
    }
    return _title;
}

/**合格*/
-(UIButton *)qualified {
    if(!_qualified) {
        _qualified = [[UIButton alloc]init];
    }
    return _qualified;
}

/**不合格*/
-(UIButton *)unqualified {
    if(!_unqualified){
        _unqualified = [[UIButton alloc]init];
    }
    return _unqualified;
}

/**不涉及*/
-(UIButton *)noRelate {
    if(!_noRelate){
        _noRelate = [[UIButton alloc]init];
    }
    return _noRelate;
}


-(void)setIsEdit:(BOOL)isEdit {
    self.qualified.userInteractionEnabled = isEdit;
    self.unqualified.userInteractionEnabled = isEdit;
    self.noRelate.userInteractionEnabled = isEdit;
}

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        self.selectionStyle=UITableViewCellSelectionStyleNone;
        // 位置lab
        [self.contentView addSubview:self.title];
        _title.frame = CGRECTS(7, 2, 60, 40);
        _title.text=@"类   型:";
        _title.font=[UIFont systemFontOfSize:17.*BIZ];
        
        // 合格
        [self.contentView addSubview:self.qualified];
        self.qualified.frame = CGRECTS(DEF_SCREEN_WIDTH / 5, 2, BTN_WIDTH, 40);
        self.qualified.tag = 100;
        [self.qualified setTitle:@"合格" forState:UIControlStateNormal];
        [self.qualified setTitleColor:[UIColor colorWithRed:0.50 green:0.50 blue:0.50 alpha:1.00] forState:UIControlStateNormal];
        self.qualified.titleLabel.font = [UIFont systemFontOfSize:17.*BIZ];
        [self.qualified.titleLabel sizeToFit];
        
        [self.qualified setImage:[UIImage imageNamed:@"weixuanzhong" ]forState:UIControlStateNormal];
        [self.qualified setImage:[UIImage imageNamed:@"xuanzhong" ] forState:UIControlStateSelected];
        [self.qualified addTarget:self action:@selector(Action:) forControlEvents:UIControlEventTouchUpInside];
        
        // 不合格
        [self.contentView addSubview:self.unqualified];
        self.unqualified.frame = CGRECTS(DEF_SCREEN_WIDTH / 5 * 2 + 1, 2, BTN_WIDTH, 40);
        self.unqualified.tag = 101;
        [self.unqualified setTitle:@"不合格" forState:UIControlStateNormal];
        [self.unqualified setTitleColor:[UIColor colorWithRed:0.50 green:0.50 blue:0.50 alpha:1.00] forState:UIControlStateNormal];
        self.unqualified.titleLabel.font=[UIFont systemFontOfSize:17.*BIZ];
        [self.unqualified.titleLabel sizeToFit];
        
        [self.unqualified setImage:[UIImage imageNamed:@"weixuanzhong" ]forState:UIControlStateNormal];
        [self.unqualified setImage:[UIImage imageNamed:@"xuanzhong" ] forState:UIControlStateSelected];
        [self.unqualified addTarget:self action:@selector(Action:) forControlEvents:UIControlEventTouchUpInside];
        
        // 不涉及
        [self.contentView addSubview:self.noRelate];
        self.noRelate.frame = CGRECTS((DEF_SCREEN_WIDTH / 5 * 3) + 10, 2, BTN_WIDTH, 40);
        self.noRelate.tag = 102;
        [self.noRelate setTitle:@"不涉及" forState:UIControlStateNormal];
        [self.noRelate setTitleColor:[UIColor colorWithRed:0.50 green:0.50 blue:0.50 alpha:1.00] forState:UIControlStateNormal];
        self.noRelate.titleLabel.font=[UIFont systemFontOfSize:17.*BIZ];
        [self.noRelate.titleLabel sizeToFit];
        
        [self.noRelate setImage:[UIImage imageNamed:@"weixuanzhong" ]forState:UIControlStateNormal];
        [self.noRelate setImage:[UIImage imageNamed:@"xuanzhong" ] forState:UIControlStateSelected];
        [self.noRelate addTarget:self action:@selector(Action:) forControlEvents:UIControlEventTouchUpInside];
        }
    return self;
}

#pragma mark -methld

- (void)Action:(UIButton *)sender{
    _lastBtn.selected = NO;
    sender.selected = !sender.selected;
    long tag = sender.tag;
    switch (tag) {
        case 100:
            if(self.clickQualifiedBlock) {
                self.clickQualifiedBlock();
            }
            break;
            
        case 101:
            if(self.clickUnqualifiedBlock){
                self.clickUnqualifiedBlock();
            }
            break;
            
        case 102:
            if(self.clickNoRelateBlock){
                self.clickNoRelateBlock();
            }
            break;
            
        default:
            break;
    }
    //选择的按钮赋值给上一个按钮
    _lastBtn = sender;
}

-(void)setBtntype:(NSString *)btntype {
    if([btntype intValue] == 1) {   // 合格
        self.qualified.selected=YES;
        _lastBtn = self.qualified;
        
    } else if ([btntype intValue] == 0) {   // 不合格
        self.unqualified.selected=YES;
        _lastBtn=self.unqualified;
        
    } else if ([btntype intValue] == 2) {
        self.noRelate.selected = YES;
        _lastBtn = self.noRelate;
    }
}

//设置分割线
- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor); CGContextFillRect(context, rect);
    //下分割线
    CGContextSetStrokeColorWithColor(context,[UIColor colorWithRed:0.89 green:0.89 blue:0.90 alpha:1.00].CGColor);
    CGContextStrokeRect(context, CGRectMake(0, rect.size.height, rect.size.width, 1));
}
@end
